package org.gap.service;

import java.util.List;
import java.util.Set;

import org.gap.dao.AdherentDAO;
import org.gap.dao.TiersDAO;

import org.gap.domain.Adherent;
import org.gap.domain.Tiers;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for Adherent entities
 * 
 */

@Service("AdherentService")
@Transactional
public class AdherentServiceImpl implements AdherentService {

	/**
	 * DAO injected by Spring that manages Adherent entities
	 * 
	 */
	@Autowired
	private AdherentDAO adherentDAO;

	/**
	 * DAO injected by Spring that manages Tiers entities
	 * 
	 */
	@Autowired
	private TiersDAO tiersDAO;

	/**
	 * Instantiates a new AdherentServiceImpl.
	 *
	 */
	public AdherentServiceImpl() {
	}

	/**
	 * Return all Adherent entity
	 * 
	 */
	@Transactional
	public List<Adherent> findAllAdherents(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<Adherent>(adherentDAO.findAllAdherents(startResult, maxRows));
	}

	/**
	 * Save an existing Tiers entity
	 * 
	 */
	@Transactional
	public Adherent saveAdherentTiers(Integer adhId, Tiers related_tiers) {
		Adherent adherent = adherentDAO.findAdherentByPrimaryKey(adhId, -1, -1);
		Tiers existingtiers = tiersDAO.findTiersByPrimaryKey(related_tiers.getTieTiers());

		// copy into the existing record to preserve existing relationships
		if (existingtiers != null) {
			existingtiers.setTieTiers(related_tiers.getTieTiers());
			existingtiers.setTieIdentClient(related_tiers.getTieIdentClient());
			existingtiers.setTieTypeTiers(related_tiers.getTieTypeTiers());
			existingtiers.setTieLibExterne(related_tiers.getTieLibExterne());
			existingtiers.setTieNationalite(related_tiers.getTieNationalite());
			existingtiers.setTieTelFix(related_tiers.getTieTelFix());
			existingtiers.setTieTelBureau(related_tiers.getTieTelBureau());
			existingtiers.setTieTelPortable(related_tiers.getTieTelPortable());
			existingtiers.setTieFax(related_tiers.getTieFax());
			existingtiers.setTieEMail(related_tiers.getTieEMail());
			existingtiers.setTieDateCreation(related_tiers.getTieDateCreation());
			existingtiers.setTieUserCreation(related_tiers.getTieUserCreation());
			related_tiers = existingtiers;
		}

		adherent.setTiers(related_tiers);
		related_tiers.getAdherents().add(adherent);
		adherent = adherentDAO.store(adherent);
		adherentDAO.flush();

		related_tiers = tiersDAO.store(related_tiers);
		tiersDAO.flush();

		return adherent;
	}

	/**
	 * Return a count of all Adherent entity
	 * 
	 */
	@Transactional
	public Integer countAdherents() {
		return ((Long) adherentDAO.createQuerySingleResult("select count(o) from Adherent o").getSingleResult()).intValue();
	}

	/**
	 * Delete an existing Tiers entity
	 * 
	 */
	@Transactional
	public Adherent deleteAdherentTiers(Integer adherent_adhId, Integer related_tiers_tieTiers) {
		Adherent adherent = adherentDAO.findAdherentByPrimaryKey(adherent_adhId, -1, -1);
		Tiers related_tiers = tiersDAO.findTiersByPrimaryKey(related_tiers_tieTiers, -1, -1);

		adherent.setTiers(null);
		related_tiers.getAdherents().remove(adherent);
		adherent = adherentDAO.store(adherent);
		adherentDAO.flush();

		related_tiers = tiersDAO.store(related_tiers);
		tiersDAO.flush();

		tiersDAO.remove(related_tiers);
		tiersDAO.flush();

		return adherent;
	}

	/**
	 * Delete an existing Adherent entity
	 * 
	 */
	@Transactional
	public void deleteAdherent(Adherent adherent) {
		adherentDAO.remove(adherent);
		adherentDAO.flush();
	}

	/**
	 * Save an existing Adherent entity
	 * 
	 */
	@Transactional
	public void saveAdherent(Adherent adherent) {
		Adherent existingAdherent = adherentDAO.findAdherentByPrimaryKey(adherent.getAdhId());

		if (existingAdherent != null) {
			if (existingAdherent != adherent) {
				existingAdherent.setAdhId(adherent.getAdhId());
				existingAdherent.setAdhCin(adherent.getAdhCin());
				existingAdherent.setAdhImmatriculation(adherent.getAdhImmatriculation());
				existingAdherent.setAdhAffiliation(adherent.getAdhAffiliation());
				existingAdherent.setAdhDateAffiliation(adherent.getAdhDateAffiliation());
				existingAdherent.setAdhPension(adherent.getAdhPension());
				existingAdherent.setAdhSom(adherent.getAdhSom());
			}
			adherent = adherentDAO.store(existingAdherent);
		} else {
			adherent = adherentDAO.store(adherent);
		}
		adherentDAO.flush();
	}

	/**
	 * Load an existing Adherent entity
	 * 
	 */
	@Transactional
	public Set<Adherent> loadAdherents() {
		return adherentDAO.findAllAdherents();
	}

	/**
	 */
	@Transactional
	public Adherent findAdherentByPrimaryKey(Integer adhId) {
		return adherentDAO.findAdherentByPrimaryKey(adhId);
	}
}
